package org.andao.commons.taglib.ui;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.andao.commons.cache.CacheAppManager;
import org.andao.commons.taglib.BaseTagSupport;
import org.andao.core.config.ConfigPropertyReader;
import org.andao.security.SpringSecurityHelper;
import org.andao.security.model.User;
import org.springframework.web.servlet.support.RequestContext;


/**
 * 前台菜单标签
 * <div id='nav'>
 * 		<ul class='ul-inline nav-menu'>
 * 			<li menu='index'><a href='${baseUrl }/edu/teacher/index.html'>主页</a></li>
 * 			<li menu='resource'><a href='${baseUrl }/edu/teacher/res/exam/list.html'>资源</a></li>
 * 			<li menu='course'><a href='${baseUrl }/edu/teacher/task/list.html'>课堂</a></li>
 * 			<li menu='app'><a href='${baseUrl }/edu/teacher/app/list.html'>应用</a></li>
 * 			<li menu='bbs'><a href='#'>论坛</a></li>
 * 		</ul>
 * 	</div>
 * 	<div class='fR'>
 * 		<div id='top-user'>
 * 
 * 			<span id='top-user-photo'><img alt='当前头像' src='photoUrl' style='width:30px;height:30px;'></span>
 * 			<a href='#' id='top-user-name'>cnName</a>
 * 
 * 			<div id='top-user-memu'>
 * 				<ul>
 * 					<li><a href='${baseUrl}/edu/teacher/user/editInfo.html'>个人资料</a></li>
 * 					<li><a href='javascript:void(0);' onclick='logout();'>退出</a></li>
 * 				</ul>
 * 			</div>
 * 		</div>
 * </div>
 * 
 * @author Seewo Software - Vick.liu (liuguanglin@cvte.cn)
 * 
 */
public class FrontMenuTag extends BaseTagSupport {

	private static final long serialVersionUID = -5327086681601918917L;


	@Override
	public int doEndTag() throws JspException {
		RequestContext ctx = new RequestContext(getrequest());
		StringBuffer menu = new StringBuffer();
		menu.append("<div id='nav'><ul class='ul-inline nav-menu'>");
		menu.append(buildMainMenu());
		menu.append("</ul></div><div class='fR'><div id='top-user'>");
		menu.append(buildUserInfo());
		menu.append("<div id='top-user-memu'><ul><li><a href='");
		menu.append(getBaseUrl());
		menu.append("/edu/teacher/user/editInfo.html'>"+ctx.getMessage("PERSON_INFO")+"</a></li><li><a href='javascript:void(0);' onclick='logout();'>"+ctx.getMessage("LOGOUT")+"</a></li></ul></div></div></div>");

		JspWriter writer = this.pageContext.getOut();
		try {
			writer.append(menu);
		} catch (IOException e) {

		}
		return EVAL_PAGE;
	}


	/**
	 * 读取权限并做成菜单
	 * <li menu='index'><a href='${baseUrl }/edu/teacher/index.html'>主页</a></li>
	 * <li menu='resource'><a href='${baseUrl }/edu/teacher/res/exam/list.html'>资源</a></li>
	 * <li menu='course'><a href='${baseUrl }/edu/teacher/task/list.html'>课堂</a></li>
	 * <li menu='app'><a href='${baseUrl }/edu/teacher/app/list.html'>应用</a></li>
	 * <li menu='bbs'><a href='#'>论坛</a></li>
	 * @return
	 * @author Seewo Software - Vick.liu (liuguanglin@cvte.cn)
	 */
	private StringBuffer buildMainMenu() {
		RequestContext ctx = new RequestContext(getrequest());
		String baseUrl = getBaseUrl();
		StringBuffer mainMenu = new StringBuffer();
		buildLiMenu(mainMenu, "index", baseUrl + "/edu/teacher/index.html", ctx.getMessage("MAIN_PAGE"), false);
		buildLiMenu(mainMenu, "resource", baseUrl + "/edu/teacher/res/exam/list.html", ctx.getMessage("RESOURCE"), false);
		buildLiMenu(mainMenu, "course", baseUrl + "/edu/teacher/task/list.html", ctx.getMessage("CLASS"), false);
		buildLiMenu(mainMenu, "app", baseUrl + "/edu/teacher/app/list.html", ctx.getMessage("APPLICATION"), false);
		buildLiMenu(mainMenu, "bbs", "#", ctx.getMessage("BBS"), false);
		return mainMenu;
	}
	
	private void buildLiMenu(StringBuffer mainMenu, String menu, String link, String name,boolean isCurrentNav) {
		mainMenu.append("<li menu='");
		mainMenu.append(menu);
		if(isCurrentNav){
			mainMenu.append("' class='nav-on'><a href='");
		}else{
			mainMenu.append("'><a href='");
		}
		mainMenu.append(link);
		mainMenu.append("'>");
		mainMenu.append(name);
		mainMenu.append("</a></li>");
	}


	/**
	 * <span id='top-user-photo'><img alt='photo' src='photoUrl' style='width:30px;height:30px;'></span>
	 * <a href='#' id='top-user-name'>cnName</a>
	 * @return
	 * @author Seewo Software - Vick.liu (liuguanglin@cvte.cn)
	 */
	private StringBuffer buildUserInfo() {
		User user = SpringSecurityHelper.getCurrentUser();
		StringBuffer infoBuffer = new StringBuffer();
		infoBuffer.append("<span id='top-user-photo'><img alt='photo' src='");
		String frontPhotoUrl = user.getPhotoURL();
		if (null != frontPhotoUrl) {
			frontPhotoUrl = getRootUrl() + CacheAppManager.getSysConfigurationByCode("web.uploadfile.rooturl").getParamValue() + "/users/" + user.getUsername() + "/" + frontPhotoUrl;
		} else {
			frontPhotoUrl = getBaseUrl() + "/" + ConfigPropertyReader.getInstance().getProperty("web.ui.image.user");
		}
		infoBuffer.append(frontPhotoUrl);
		infoBuffer.append("' style='width:30px;height:30px;'></span><a href='#' id='top-user-name'>");
		infoBuffer.append(user.getCnName());
		infoBuffer.append("</a>");
		return infoBuffer;
	}

}
